﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Logic;
using Portal.Entity;
using Portal.Interface;
using MyLib;
using System.Threading;

namespace Portal
{
    public partial class SecurityDetail : System.Web.UI.Page
    {
        SecurityLogic _logic = new SecurityLogic();
        ResourceLogic _res = new ResourceLogic();
        ResourceRelationLogic _relation = new ResourceRelationLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtResourceId.Text = Request.QueryString["ResourceId"];

                List<string> securityTypes = _logic.GetSecurityTypeList();
                ddlType.DataSource = securityTypes;
                ddlType.DataBind();
                List<ResourceRelationEntity> menus = new List<ResourceRelationEntity>();

                //根据ResourceId加载数据
                LoadData(out menus);

                LoadMenu(menus);

                      
                ddlType_SelectedIndexChanged(sender, e);

                btnSave.Text = Portal.Resources.lang.SecurityDetail_Save;
            }

            txtMember.Attributes.Add("readonly", "readonly");
        }

        void LoadData(out List<ResourceRelationEntity> menus)
        {
            ResourceEntity ety= _res.GetEntity(ConvertUtil.ToInt32( txtResourceId.Text));
            if (ety == null)
            {
                menus = new List<ResourceRelationEntity>();
                return;
            }
            txtName.Text = ety.CNNAME;
            txtMember.Text = ety.EXT01;
            txtMemberId.Text = ety.EXT02;
            List<ResourceRelationEntity> list= _relation.GetList(ConvertUtil.ToInt32(txtResourceId.Text));
            menus = list.FindAll(p => p.RELATIONTYPE == "SecurityObject" && p.EXT01=="Menu");            
        }

        void LoadMenu(List<ResourceRelationEntity> menus)
        {
            List<ResourceEntity> list = _res.GetResourceList("Menu");
            //加第一层节点
            List<ResourceEntity> rootlist = list.FindAll(p => p.PARENTID == 0);
            foreach (ResourceEntity ety in rootlist)
            {
                TreeNode tn = new TreeNode();
                if (Portal.Resources.lang.ResourceNameField.ToUpper() == "CNNAME")
                {
                    tn.Text = ety.CNNAME;
                }
                else
                {
                    tn.Text = ety.ENNAME;
                }
                tn.Value = ety.RESOURCEID.ToString();
                if (menus.Exists(p => p.DESTINATIONID == ety.RESOURCEID))
                {
                    tn.Checked = true;
                }
                tn.NavigateUrl = "#";
                tvMenu.Nodes.Add(tn);

                //递归插入子节点
                AddChildNodes(menus,tn, ety.RESOURCEID, list);
            }
            tvMenu.ExpandAll();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ResourceRelationEntity> menus = new List<ResourceRelationEntity>();
            List<ResourceRelationEntity> objs = _relation.GetList(ConvertUtil.ToInt32(txtResourceId.Text));
            menus = objs.FindAll(p => p.RELATIONTYPE == "SecurityObject" && p.EXT01 == ddlType.SelectedValue);    

            List<ResourceEntity> list = _res.GetResourceList(ddlType.SelectedValue);
            tvData.Nodes.Clear();
            //加第一层节点
            List<ResourceEntity> rootlist= list.FindAll(p => p.PARENTID == 0);
            foreach (ResourceEntity ety in rootlist)
            {
                TreeNode tn = new TreeNode();
                if (Portal.Resources.lang.ResourceNameField.ToUpper() == "CNNAME")
                {
                    tn.Text = ety.CNNAME;
                }
                else
                {
                    tn.Text = ety.ENNAME;
                }
                tn.Value = ety.RESOURCEID.ToString();
                tn.NavigateUrl = "#";
                if (menus.Exists(p => p.DESTINATIONID == ety.RESOURCEID))
                {
                    tn.Checked = true;
                }
                tvData.Nodes.Add(tn);

                //递归插入子节点
                AddChildNodes(menus,tn, ety.RESOURCEID, list);
            }
            tvData.ExpandAll();
        }

        void AddChildNodes(List<ResourceRelationEntity> menus,TreeNode parent, int id,List<ResourceEntity>  list)
        {
            List<ResourceEntity> childlist = list.FindAll(p => p.PARENTID == id);
            foreach (ResourceEntity ety in childlist)
            {
                TreeNode tn = new TreeNode();
                if (Portal.Resources.lang.ResourceNameField.ToUpper() == "CNNAME")
                {
                    tn.Text = ety.CNNAME;
                }
                else
                {
                    tn.Text = ety.ENNAME;
                }
                tn.Value = ety.RESOURCEID.ToString();
                tn.NavigateUrl = "#";
                if (menus.Exists(p => p.DESTINATIONID == ety.RESOURCEID))
                {
                    tn.Checked = true;
                }
                parent.ChildNodes.Add(tn);

                AddChildNodes(menus,tn, ety.RESOURCEID, list);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SecurityLogic logic = new SecurityLogic();
            List<SecurityMemberEntity> members=new List<SecurityMemberEntity>();
            string[] sz=txtMemberId.Text.Split(',');
            foreach(string str in sz)
            {
                SecurityMemberEntity ety= new SecurityMemberEntity();
                string[] ss = str.Split('|');
                ety.MemberId=ConvertUtil.ToInt32(ss[0]);
                if (ss.Length > 1)
                {
                    ety.MemberType = ss[1];
                }
                members.Add(ety);
            }
            List<ResourceEntity> objects = new List<ResourceEntity>();
            List<TreeNode> tns = GetAllCheckedNodes(tvMenu);
            foreach (TreeNode tn in tns)
            {
                ResourceEntity ety = new ResourceEntity();
                ety.TYPE = "Menu";
                ety.RESOURCEID =ConvertUtil.ToInt32(tn.Value);
                ety.CNNAME = tn.Text;
                ety.ENNAME = tn.Text;
                objects.Add(ety);
            }
            tns = GetAllCheckedNodes(tvData);
            foreach (TreeNode tn in tns)
            {
                ResourceEntity ety = new ResourceEntity();
                ety.TYPE = ddlType.SelectedValue;
                ety.RESOURCEID = ConvertUtil.ToInt32(tn.Value);
                ety.CNNAME = tn.Text;
                ety.ENNAME = tn.Text;
                objects.Add(ety);
            }
            int resourceId=logic.SaveSecurity(ConvertUtil.ToInt32(txtResourceId.Text), txtName.Text,txtMember.Text,txtMemberId.Text, members,ddlType.SelectedValue, objects);
            txtResourceId.Text = resourceId.ToString();

            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('"+Portal.Resources.lang.SecurityDetail_SaveSuccess+"!');location.href='SecurityList.aspx';", true);

        }

        List<TreeNode> GetAllCheckedNodes(TreeView tv)
        {
            List<TreeNode> nodes = new List<TreeNode>();
            foreach (TreeNode tn in tv.Nodes)
            {
                if (tn.Checked)
                {
                    nodes.Add(tn);
                }
                GetChild(nodes, tn);
            }
            return nodes;
        }

        void GetChild(List<TreeNode> tns, TreeNode tn)
        {
            foreach (TreeNode t in tn.ChildNodes)
            {
                if (t.Checked)
                {
                    tns.Add(t);
                }
                GetChild(tns, t);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SecurityList.aspx");
        }
    }
}